package org.msfj.admin.setting.controller;

import org.msfj.common.base.BaseController;
import org.msfj.common.model.CommonResult;
import org.msfj.common.model.RedisResult;
import org.msfj.common.util.CacheUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 系统redis缓存信息controller
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-06-13 14:34:21
 * @Version: 1.0
 * @Created in idea by autoCode
 */
@Api(value = "系统redis缓存信息接口", tags = "系统redis缓存信息接口")
@RestController
@RequestMapping("setting/cache")
public class CacheController extends BaseController {

    /**
     * 添加或修改redis缓存信息
     *
     * @param key
     * @return CommonResult.ok(DeptVo)
     */
    @RequestMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "添加或修改redis缓存信息", notes = "添加或修改redis缓存信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "key", name = "key", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(value = "value", name = "value", dataType = "java.lang.String", paramType = "query"),
    })
    public CommonResult editCache(String key, String value) {
        CacheUtil.getCache().set(key, value);
        return CommonResult.ok();
    }

    /**
     * 删除redis缓存信息
     *
     * @param keys
     * @return CommonResult.ok()
     */
    @RequestMapping("/del")
    @ResponseBody
    @ApiOperation(value = "删除redis缓存信息", notes = "删除redis缓存信息")
    @ApiImplicitParam(value = "key", name = "key", dataType = "java.lang.String", paramType = "query")
    public CommonResult delCache(@RequestParam(value = "keys[]", required = false) List<String> keys) {
        CacheUtil.getCache().dels(keys);
        return CommonResult.ok();
    }

    /**
     * 清空redis缓存信息
     *
     * @return CommonResult.ok()
     */
    @RequestMapping("/flush")
    @ResponseBody
    @ApiOperation(value = "清空redis缓存信息", notes = "清空redis缓存信息")
    public CommonResult flushCache() {
        CacheUtil.getCache().delAll("*");
        return CommonResult.ok();
    }

    /**
     * 根据翻页参数、搜索参数查找redis缓存信息
     *
     * @param key 关键字
     * @return CommonResult.ok(>)
     */
    @RequestMapping("/list")
    @ResponseBody
    @ApiOperation(value = "根据翻页参数、搜索参数查找redis缓存信息", notes = "根据翻页参数、搜索参数查找redis缓存信息")
    @ApiImplicitParam(value = "key", name = "key", dataType = "java.lang.String", paramType = "query")
    public CommonResult listCache(String key) {
        List<RedisResult> allResult = CacheUtil.getCache().getAllResult(key);
        return CommonResult.ok(allResult);
    }
}
